<?php

namespace register\view;

require_once("./common/Filter.php");
require_once("./register/model/RegisterObserver.php");
require_once("./common/model/ExMsg.php");
require_once("./common/model/ErMsg.php");

class RegisterUserView implements \register\model\RegisterObserver {
	
	private static $REGISTERUSER = "register";
	private static $USERNAME = "RegisterUserView::Username";
	private static $PASSWORDONE = "RegisterUserView::PasswordOne";
	private static $PASSWORDTWO = "RegisterUserView::PasswordTwo";
	private static $REGISTERBUTTON = "RegisterUserView::RegisterButtonID";
	
	private $messageUsername = "";
	private $messagePassword = "";
	private $messageRegistered = "";
	
	public function getRegisterUserBox() {
		return "
			<form action='?" . self::$REGISTERUSER . "&var=1' method='post' enctype='multipart/form-data'>
				<fieldset>
					<p>$this->messageUsername</p>
					<p>$this->messagePassword</p>
					<legend>Registrera ny användare - Skriv in användarnamn och lösenord</legend>
					<label for='" . self::$USERNAME . "' >Namn :</label>
					<input type='text' size='20' name='" . self::$USERNAME . "' id='UserNameID' value='" . $this->getRawUsername() . "' /></br>
					<label for='" . self::$PASSWORDONE . "' >Lösenord  :</label>
					<input type='password' size='20' name='" . self::$PASSWORDONE . "' id='PasswordID' /></br>
					<label for='" . self::$PASSWORDTWO . "' >Repetera Lösenord  :</label>
					<input type='password' size='20' name='" . self::$PASSWORDTWO . "' id='PasswordID' /></br>
					<label for='" . self::$REGISTERBUTTON . "' >Skicka :</label>
					<input type='submit' name='' id='" . self::$REGISTERBUTTON . "'  value='Registrera' />
				</fieldset>
			</form>";
	}
	
	public function isRegisteringUser() {
		return isset($_GET["var"]);
	}
	
	public function RegisterFailed($e) {
		if (strlen($this->getRawUsername()) < 3) {
			$this->messageUsername = \common\model\ErMsg::$UsernameToShort;
		}
		else if (strlen($this->getRawUsername()) > 9) {
			$this->messageUsername = \common\model\ErMsg::$UsernameToLong;
		}
		else if ($e == \common\model\ErMsg::$UsernameAlreadyExists) {
			$this->messageUsername = $e;
		}
		else if ($e == \common\model\ErMsg::$UsernameHasTags) {
			$this->messageUsername = $e;
		}
		
		if (strlen($this->getPassword()) < 6) {
			$this->messagePassword = \common\model\ErMsg::$PasswordToShort;
		}
		else if (strlen($this->getPassword()) > 16) {
			$this->messagePassword = \common\model\ErMsg::$PasswordToLong;
		}
		else if ($this->getPassword() != $this->getOtherPassword())
		{
			$this->messagePassword = \common\model\ErMsg::$PasswordDoNotMatch;
		}
	}
	
	public function RegisterOK() {
		header("Location: index.php");
	}
	
	/**
	 * @return String
	 */
	public function getUsername() {
		if (isset($_POST[self::$USERNAME])) {
			if(\Common\Filter::hasTags($_POST[self::$USERNAME])) {
				throw new \Exception(\common\model\ErMsg::$UsernameHasTags);
			}
			return \Common\Filter::sanitizeString($_POST[self::$USERNAME]);	
		}
		else
			return "";
	}
	
	/**
	 * @return String
	 */
	public function getRawUsername() {
		if (isset($_POST[self::$USERNAME])) {
			return \Common\Filter::sanitizeString($_POST[self::$USERNAME]);	
		}
		else
			return "";
	}
	
	/**
	 * @return String
	 */
	public function getPassword() {
		if (isset($_POST[self::$PASSWORDONE]))
			return \Common\Filter::sanitizeString($_POST[self::$PASSWORDONE]);
		else
			return "";
	}
	
	/**
	 * @return String
	 */
	public function getOtherPassword() {
		if (isset($_POST[self::$PASSWORDTWO]))
			return \Common\Filter::sanitizeString($_POST[self::$PASSWORDTWO]);
		else
			return "";
	}
	
	/**
	 * @return UserCredentials
	 */
	public function getUserCredentials() {
		return \login\model\UserCredentials::create(new \login\model\UserName($this->getUserName()), 
														\login\model\Password::fromCleartext($this->getPassword()));
	}
}
